using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace DemonHunter
{
    /// <summary>
    /// Event queue will store events in order they should happen. 
    /// EventQueue fills a levels entity list with the entity's that it should have for this event
    /// EventQueue fills a levels misc bitmap list with bitmaps for use
    /// 11/22/2006 TO DO: 
    ///     how to handle misc bitmaps for event.  they need initial locations and bitmaps
    ///     decide how to couple that information
    /// </summary>
    class EventQueue
    {
        //data
        private Queue<Event> levelEventQueue;
        private XmlTextReader reader;
        private Game game;
        /// <summary>
        /// Gets the initial event and loads it into memory
        /// </summary>
        /// <param name="newEvent"></param>
        /// <param name="game"></param>
        public EventQueue(string fileName)
        {
            game = Game.GetInstance();
            loadFile(fileName);
        }
        public void AddEvent(Event newEvent)
        {
            levelEventQueue.Enqueue(newEvent);
        }
        public Event getNextEvent()
        {
            return levelEventQueue.Dequeue();
        }

        public double getNextEventXLoc()
        {
            if (isEmpty()) return -1;
            Event nextEvent = levelEventQueue.Peek();
            return nextEvent.getEventxLoc();
        }

        public bool isEmpty()
        {
            if (levelEventQueue.Count == 0)
            {
                return true;
            }
            else { return false; }
        }
        /// <summary>
        /// Parses an XML file.  --NOTE-- refer to Level.cs for key on level bitmap array
        /// </summary>
        /// <param name="newEvent"></param>
        public void loadFile(string fileName)
        {
            levelEventQueue = new Queue<Event>();
            Event newEvent = new Event();

            using (reader = new XmlTextReader(fileName))
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.LocalName == "xLoc")
                        {
                            newEvent.setEventxLoc(reader.ReadElementContentAsDouble());
                        }
                        if (reader.LocalName == "prop")
                        {
                            string tmpStr = reader.ReadElementContentAsString();
                            if (tmpStr == "bolt")
                            {
                                newEvent.setType(Event.EVENT_BOLT);
                                levelEventQueue.Enqueue(newEvent);
                                newEvent = new Event();
                            }
                            else if (tmpStr == "cows")
                            {
                                newEvent.setType(Event.EVENT_COWS);
                                levelEventQueue.Enqueue(newEvent);
                                newEvent = new Event();
                            }
                            else if (tmpStr == "portal")
                            {
                                newEvent.setType(Event.EVENT_PORTAL);
                                levelEventQueue.Enqueue(newEvent);
                                newEvent = new Event();
                            }
                        }
                        if (reader.LocalName == "entity")
                        {
                            string tmpStr = reader.ReadElementContentAsString();
                            if (tmpStr == "skeleton")
                            {
                                newEvent.setType(Event.EVENT_SKELETON);
                                levelEventQueue.Enqueue(newEvent);
                                newEvent = new Event();
                            }
                            else if (tmpStr == "minotaur")
                            {
                                newEvent.setType(Event.EVENT_MINOTAUR);
                                levelEventQueue.Enqueue(newEvent);
                                newEvent = new Event();
                            }
                            else if (tmpStr == "biker")
                            {
                                newEvent.setType(Event.EVENT_BIKER);
                                levelEventQueue.Enqueue(newEvent);
                                newEvent = new Event();
                            }
                        }
                    }
                }
            }
        }
    }
}
